var isSign=false;         //防止重复点击
var resourceUrl = getApp().request("imgUrl");
var requestUrl = getApp().request("url");
var header = getApp().request("header");
var app = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    days:0,
    isSignIn:"",
    signGiftBox:false,
    resourceUrl: resourceUrl,
    coin5000: false,
    today7Sign:false,
    latitude:"",
    longitude:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '',
      mask:true
    })
    var that=this;
    //请求签到情况
    wx.request({
      url: requestUrl+'findSignInfo',
      method: "POST",
      data: { userId: app.session_key.userId, salt: app.session_key.salt},
      header: header ,
      success: (res) => {
        console.log(res)
        if (res.statusCode != 200) {
          getApp().showModal("服务器异常");
          return
        }
        wx.hideLoading()
        if(res.data.code==200){
          var isSignIn = res.data.data.today == 1 ? "今日已签" :"立即签到"
          var days = res.data.data.days;
          if (res.data.data.today==0){
             days = days==7?0:days;
          }
          if (res.data.data.today == 1 && res.data.data.days==7){
            that.setData({
              coin5000: true,
              today7Sign: true
            })
          }
          this.setData({
            days:days,
            isSignIn: isSignIn
          });
        } else if (res.data.code == 401 || res.data.code == 301 || res.data.code == 302) {
          console.log(res.data.code)
            getApp().showModal("系统信息有误，请重试！");
            this.setData({
              isSignIn: "今日已签"
            })
        } else if (res.data.code == 800) {
          getApp().showModal(res.data.data.msg);
          wx.hideLoading();
        }
      },
      fail: (fai) => {
        console.log("加载进来请求签到情况fail");
        console.log(fai)
        getApp().showModal("网络超时，请检查您的网络");
      }
    })
  },
  btnSignIn: function () {
    if(isSign){return};
    isSign=true;
    if (this.data.isSignIn == "今日已签") { return }
    var that = this;
    wx.showLoading({
      title: '',
      mask: true
    })
    //点击签到
    wx.request({
      url: requestUrl +'sign',
      method: "POST",
      data: { userId: app.session_key.userId, salt: app.session_key.salt, longitude: that.data.longitude, latitude: that.data.latitude },
      header: header,
      success: function (res) {
        console.log(res)
        if (res.statusCode != 200) {
          getApp().showModal("服务器异常");
          return
        }
        wx.hideLoading();
        if (res.data.code == 200) {
          var days = that.data.days;
          days++
          that.setData({
            days: days,
            isSignIn: "今日已签",
            signGiftBox:true
          });
          //本地存储中初级礼盒加一个
          var juniorGiftBoxN = app.juniorGiftBoxN;
          juniorGiftBoxN++;
          app.juniorGiftBoxN=juniorGiftBoxN;
          //签到7天加5000金币
          if(res.data.data.days==7){
            var allGoldNum = app.gold;
            allGoldNum += 5000;
            app.gold=allGoldNum;
            that.setData({
              coin5000:true,
              today7Sign:true
            })
          }
        } else if (res.data.code == 401 || res.data.code == 301 || res.data.code == 302){
          console.log(res.data.code)
          isSign=false;
          getApp().showModal("提交信息有误！");
        } else if (res.data.code == 201 ) {
          isSign = false;
          getApp().showModal("今日已经签到！");
        } else if (res.data.code == 303) {
          isSign = false;
          wx.navigateTo({
            url: '../login/login?url=signIn'
          })
        } else if (res.data.code == 800) {
          getApp().showModal(res.data.data.msg);
          wx.hideLoading();
        }
      },
      fail: (fai) => {
        console.log("点击签到请求接口fail");
        console.log(fai)
        wx.hideLoading();
        getApp().showModal("网络超时，请检查您的网络");
      }
    })
    
  },
  btnEnsureSignGiftBox(){
    this.setData({
      signGiftBox: false
    });
    wx.navigateTo({
      url: '../warehouse/warehouse',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    isSign=false;
    wx.getLocation({
      type: 'wgs84',
      success:(res)=> {
        this.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
      }
    });
    wx.showShareMenu({
      withShareTicket: true
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    //是否选车
    var fightCarTypeId = app.fightCarTypeId;
    //随机一句宣言
    var declaration = getApp().Refit();
    var myRefitTxt = Math.floor(Math.random() * declaration.length);
    
    if (fightCarTypeId == 0) {
      return {
        title: getApp().shareTitle1(),
        path: "/pages/index/index"
      }
    } else {
      return {
        title: getApp().shareTitle2(),
        path: "/pages/openShare/openShare?shareId=" + app.session_key.userId + "&carTypeId=" + fightCarTypeId + "&shareRefitNum=" + myRefitTxt + "&carName=SUV概念车",
        success: (re) => {
          if (Object.keys(re).length == 1) {
            return
          }
          var ticket = re.shareTickets[0];
          getApp().shareFn(ticket);
        }
      }
    }
  }
})